package com.ruoyi.web.controller.hospital;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;


import com.ruoyi.hospital.domain.BaseQuery;
import com.ruoyi.hospital.domain.Dept;
import com.ruoyi.hospital.service.IHospitalDeptService;
import com.ruoyi.hospital.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api("医院信息管理")
@RestController
@RequestMapping("/hospital/dept")
public class DeptController extends BaseController {
    @Autowired
    private IHospitalDeptService hospitalDeptService;
    @ApiOperation("所有部门用户列表")

    @PostMapping("/list")
    public AjaxResult list(  ){
          return AjaxResult.success(hospitalDeptService.findAll());
    }

    /**
     * 分页
     * @return
     */
    @ApiOperation("分页查询")
    @CrossOrigin
    @PostMapping("/listPage")
    public AjaxResult pageList(@RequestBody BaseQuery query){
        PageList<Dept> list = hospitalDeptService.queryPage(query);
        return AjaxResult.success(list);
    }



    @ApiOperation("查询一条部门用户")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable Long deptId)
    {
        return AjaxResult.success(hospitalDeptService.selectDeptById(deptId));
    }

    @ApiOperation("新增和修改")
    @PostMapping
    public AjaxResult addOrUpdate(@RequestBody Dept dept){
        try {
            if( dept.getId()!=null)
            {hospitalDeptService.update(dept);}
            else {
                System.out.println(dept);
                hospitalDeptService.save(dept);}
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success();
        }
    }
    /**
     * 删除对象信息
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            hospitalDeptService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success();
        }
    }

    /**
     * 无限极树
     * @param
     * @return
     */
    @ApiOperation("无限极树")
    @GetMapping("/tree")
    public List<Dept> queryTree(){
        List<Dept> tree = hospitalDeptService.queryTree();
        return tree;
    }
    @ApiOperation("表格导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response)
    {
        List<Dept> list = hospitalDeptService.findAll();
        ExcelUtil<Dept> util = new ExcelUtil<Dept>(Dept.class);
        util.exportExcel(response, list, "医院科室管理数据");
    }



//    /**
//     *  模糊查询
//     * @param name
//     * @return
//     */
//    @ApiOperation("科室搜索")
//    @GetMapping(value="/query/{name}")
//    public AjaxResult selectByName(@PathVariable("name")  String name) {
//        List<Dept> deptByName = hospitalDeptService.selectDeptByName(name);
//        return AjaxResult.success(deptByName);
//    }


}
